package com.auerdi.ui.detail.address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.ui.account.ADADDresssAddFragment;
import com.auerdi.ui.account.evemt.RefleshAddressEvemt;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.AddressLists;
import com.example.egghttp.retrofit.beans.goods.Address;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADDressShowFragMent extends SwipeBackFragment {

    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.recy)
    RecyclerView recy;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.addAddressBtn)
    TextView addAddressBtn;
    List<Address> mAddressList = new ArrayList<>();

    public static ADDressShowFragMent newInstance() {

        Bundle args = new Bundle();
        ADDressShowFragMent fragment = new ADDressShowFragMent();
        fragment.setArguments(args);
        return fragment;
    }

    Unbinder unbinder;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.address_fragment, container, false);
        L.d("hcia", "ADDressShowFragMent onCreateView:");

        unbinder = ButterKnife.bind(this, view);
        titleLeft.setOnClickListener(v -> pop());

        addAddressBtn.setOnClickListener(v -> EventBus.getDefault().post(ADADDresssAddFragment.newInstance("", "", "", "", new Address())));
        View tpview = attachToSwipeBack(view);
        return tpview;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


        refleshData();
    }

    private void refleshData() {
        RetrofitClient.getPDAClient().create(AdService.class).
                addressLists()
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<AddressLists>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        L.d("hcia", "e:" + e);
                    }

                    @Override
                    public void onNext(BOb<AddressLists> addressListsBOb) {

                        List<Address> addressList = addressListsBOb.getBody().getAddress_list();
//
//                        if (addressList.size() == 0) {
//                            EventBus.getDefault().post(ADADDresssAddFragment.newInstance("",""));
//                        }

                        mAddressList.clear();
                        mAddressList.addAll(addressList);
                        recy.getAdapter().notifyDataSetChanged();

//                        L.d("hcia", " addressList.size():" + addressList.size());
                    }
                });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recy.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recy.setAdapter(new CommonAdapter<Address>(_mActivity, R.layout.item_addredd_a, mAddressList) {
            @Override
            protected void convert(ViewHolder viewHolder, final Address item, int position) {

                viewHolder.setText(R.id.name, item.getConsignee());
                viewHolder.setText(R.id.phone, item.getMobile());
                viewHolder.setText(R.id.addredd, item.getAddress());

                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        L.d("hcia", " item.getAddress_id():" + item.getAddress_id());

//                        EventBus.getDefault().post(new AddressIdEvent(item.getAddress_id()));
                        EventBus.getDefault().post(item);
                        pop();

                    }
                });

                viewHolder.getView(R.id.edit_address).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        EventBus.getDefault().post(ADADDresssAddFragment.newInstance(item.getAddress_id(), item.getMobile(), item.getConsignee(), item.getAddress(), item));

                    }
                });

            }
        });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().post(new RefleshAddressEvemt());
        unbinder.unbind();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RefleshAddressEvemt address) {

        refleshData();


    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }


}
